package com.zenchn.aspect;

import com.zenchn.aspect.service.BuildCriticalOperLogService;
import com.zenchn.enums.SessionAttributeEnum;
import com.zenchn.model.*;
import com.zenchn.utils.IPUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * AccountControllerAspect：
 * 用户管理- 关键操作AOP
 * @author panbingqi
 * @date 2021-06-01
 */
@Aspect
@Order(2)
@Component
public class CriticalOperLogAspect {


    @Resource
    private BuildCriticalOperLogService buildCriticalOperLogService;


    /**
     * 账户管理-删除 切面
     * @param joinPoint
     * @param ret
     * @throws Exception
     */
    @AfterReturning(returning = "ret", pointcut = "execution(public * com.zenchn.controller.AccountController.delete(..))")
    public void doAccountControllerDelete(JoinPoint joinPoint, Object ret) throws Exception {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();

        final TSysAccount currAccount = (TSysAccount) request.getSession().getAttribute(SessionAttributeEnum.CURRENT_ACCOUNT_INFO.getKey());

        buildCriticalOperLogService.accountDelete(currAccount,IPUtils.getRealIP(request),(Map<String, String>)request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE));

    }

}
